//
// Created by robin on 4/7/16.
//

#include "Column.h"
#include "DataType.h"
#include "SQLFlag.h"

namespace duomai {
    namespace im {

        Column::Column(const string column_name) :
                columnName(column_name),
                columnType(SQLITEWRAPPER_TEXT),
                columnFlag(FLAG_NONE),
                index_(-1) {}

        Column::Column(const string column_name, DataType column_type,
                       int flags) :
                columnName(column_name),
                columnType(column_type),
                columnFlag(flags),
                index_(-1) {}

        const string Column::getColumnName() const {
            return columnName;
        }

        DataType Column::getColumnType() const {
            return columnType;
        }

        const string Column::getColumnTypeString() const {

            switch (columnType) {
                case SQLITEWRAPPER_INTEGER: return "INTEGER";
                case SQLITEWRAPPER_TEXT: return "TEXT";
                case SQLITEWRAPPER_FLOAT: return "REAL";
                default: return "";
            }

        }

        int Column::getColumnFlag() const {
            return columnFlag;
        }

        Column & Column::operator=(const Column& column) {
            if (this != &column) {
                this->columnName = column.columnName;
                this->columnType = column.columnType;
                this->columnFlag = column.columnFlag;
            }

            return *this;
        }

        Column::Column(const Column &column) :
        columnName(column.columnName),
        columnType(column.columnType),
        columnFlag(column.columnFlag) {}

        bool Column::isPrimaryKey() const {
	        return ((columnFlag & FLAG_PRIMARY_KEY) == FLAG_PRIMARY_KEY);
        }


        bool Column::isNotNULL() const {
	        return ((columnFlag & FLAG_NOT_NULL) == FLAG_NOT_NULL);
        }

        bool Column::isUnique() const {
            return ((columnFlag & FLAG_UNIQUE) == FLAG_UNIQUE);
        }

        int Column::getIndex() const {
            return index_;
        }

        void Column::setIndex(int index) {
            index_ = index;
        }

        string Column::toSQLCreate() {

            string value = getColumnName() + " " + getColumnTypeString();

            if (isPrimaryKey())
                value += " PRIMARY KEY";

            if (isNotNULL())
                value += " NOT NULL";

            if (isUnique())
                value += " UNIQUE";

            return value;
        }
    }
}

